
import { createStore } from "vuex";
import createPersistedState from 'vuex-persistedstate';

export default createStore({
    
    state() {
        return {
            count: 0,
            commonId: {id: 0},
            curUser: 'T00001',
            mindOptions: {
                draggable: true,
                editable: true,
                contextMenu: true,
                toolBar: false,
                nodeMenu: true,
                keypress: true,
                allowUndo: true,
            },
            curLabel: null,
            isCreateGroup: false,
            isShowCreateButton: true,
            isCreate: false,
            isEdit: false,
            isImport: false,
            isExport: false,
            wsConnections: [],
        }
    },
    // 负责管理 state 中的数据，并进行修改
    mutations: {
        changeCommonId(state, value){
            // console.log("The id was update to:", value)
            state.commonId.id = value
        },
        changeMindOptions(state, key, value){
            state.mindOptions[key] = value
        },
        changeCurUser(state, value) {
            state.curUser = value
        },
        changeCurLabel(state, value){
            state.curLabel = value
        },
        changeIsCreateGroup(state, value){
            state.isCreateGroup = value
        },
        changeIsShowCreateButton(state, value){
            state.isShowCreateButton = value
        },
        changeIsCreate(state, value){
            state.isCreate = value
        },
        changeIsEdit(state, value){
            state.isEdit = value
        },
        changeIsImport(state, value){
            state.isImport = value
        },
        changeIsExport(state, value){
            state.isExport = value
        },
        changeWsConnections(state, value){
            state.wsConnections = value
        }
    },
    // 负责对数据的调度，将最终的数据交由mutations去处理，业务逻辑写在此处
    actions: {
        changeStateAsync({content, value}) {
            content.commit("changeState", value)
        }
    },
    getters: {
        getCommonId(state){
            return state.commonId
        },
        getCurUser(state){
            return state.curUser
        },
        getOptions(state, key){
            if (key == 'draggable'){
                return state.mindOptions.draggable
            }
            if (key == 'editable'){
                return state.mindOptions.editable
            }
            if (key == 'contextMenu'){
                return state.mindOptions.contextMenu
            }
            if (key == 'toolBar'){
                return state.mindOptions.toolBar
            }
            if (key == 'nodeMenu'){
                return state.mindOptions.nodeMenu
            }
            if (key == 'keypress'){
                return state.mindOptions.keypress
            }
            if (key == 'allowUndo'){
                return state.mindOptions.allowUndo
            }
            return state.mindOptions
        },
        getCurLabel(state) {
            return state.curLabel
        },
        getIsShowCreateButton(state){
            return state.isShowCreateButton
        },
        getIsCreate(state){
            return state.isCreate
        },
        getIsCreateGroup(state){
            return state.isCreateGroup
        },
        getIsEdit(state){
            return state.isEdit
        },
        getIsImport(state){
            return state.isImport
        },
        getIsExport(state){
            return state.isExport
        },
        getWsConnections(state){
            return state.wsConnections
        }
    },
    plugins: [createPersistedState()]
})
